// 主动触发窗口变化事件，让图表根据窗口变化而自适应
export const changeWindowSize = () => {
    if (document.createEvent) {
        const event = document.createEvent('HTMLEvents')
        event.initEvent('resize', true, true)
        window.dispatchEvent(event)
    } else if (document.createEventObject) {
        window.fireEvent('onresize')
    }
}
// 阻止冒泡和默认行为
export const stopBubbleDefault = e => {
    e.stopPropagation ? e.stopPropagation() : window.event.cancelBubble = true
    e.preventDefault ? e.preventDefault() : e.returnValue = false
}

/**
 * 判断obj是否为空
 * @param obj
 * @returns {boolean}
 */
export const isEmpty = (obj) => {
    if (typeof obj === 'undefined') {
        return true
    } else {
        if (obj == null) {
            return true
        }
        if (typeof obj === 'string' && obj === '') {
            return true
        } else if (obj instanceof Object && Object.keys(obj).length === 0) {
            return true
        } else if (obj instanceof Array && obj.length === 0) {
            return true
        }
        return false
    }
}
/**
 * 将sourceObj中的属性复制到targetObj中
 * @param targetObj
 * @param sourceObj
 * @returns {{}}
 */
export const mergeObject = (targetObj, sourceObj) => {
    if (isEmpty(sourceObj)) {
        return isEmpty(targetObj) ? {} : targetObj
    }
    for (let p in targetObj) {
        if (sourceObj.hasOwnProperty(p) && !isEmpty(sourceObj[p])) {
            targetObj[p] = sourceObj[p]
        }
    }
}

/**
 * 将list转换为map
 * @param list 需要转换的list
 * @param key  map的key字段
 * @param value  map的value字段，如果不存在则使用整个对象
 */
export const convertList2Map = (list, key, value) => {
    let result = {}
    if (isEmpty(list)) {
        return result
    }
    for (let i = 0; i < list.length; i++) {
        if (list[i].hasOwnProperty(key)) {
            if (value && list[i].hasOwnProperty(value)) {
                result[list[i][key]] = list[i][value]
            } else {
                result[list[i][key]] = list[i]
            }
        }
    }
    return result
}
/**
 * 将list转换为tree结构
 * @param list
 * @param parentId  父节点字段名
 * @param id        节点字段名
 * @param name      label字段名
 * @param root      根节点值
 * @returns {Array}
 */
export const list2Tree = (list, parentId, id, name, root) => {
    let result = []
    for (let item of list) {
        if (item[parentId] === root) {
            let node = {
                id: item[id],
                name: item[name],
                data: item,
                iconType: item.iconType,
                isFolder: item.isFolder,
                children: list2Tree(list, parentId, id, name, item[id])
            }
            result.push(node)
        }
    }
    return result
}
export const colors = {
    'icon-table': '#00BB51',
    'icon-read-table': '#00BB51',
    'icon-module-preprocess': '#E75751',
    'icon-module-type-transform': '#E75751',
    'icon-module-data-split': '#E75751',
    'icon-module-sql': '#00B7e5',
    'icon-module-binary-classification': '#4754B5',
    'icon-module-regression': '#4754B5',
    'icon-module-clustering': '#4754B5',
    'icon-module-training': '#4754B5',
    'icon-module-tuning': '#4754B5',
    'icon-module-prediction': '#4754B5',
    'icon-module-evaluation': '#4754B5',
    'icon-module-cv': '#4754B5',
    'icon-tezhengzhuanhuan': '#EF8C41',
    'icon-tezhenggongcheng': '#EF8C41',
    'icon-shuangbianliangfenxi': '#F0CD15',
    'icon-danbianliangfenxi': '#F0CD15',
    'icon-guanlianfenxi': '#3479BF',
    'icon-shujubiaodaoru': '#00BB51',
    'icon-shujubiaotongbu': '#00BB51',
    'icon-shujubiaodaochu': '#00BB51'
}
/**
 * @param arr
 * @param id        节点字段名
 * @param label      label字段名
 * @returns {Array}
 */
export const folderList = (arr, id, label) => {
    let newArr = []
    const getChildren = (children) => {
        if (children && children.length) {
            return folderList(children, id, label) || []
        }
    }
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].isFolder) {
            let item = {
                id: arr[i][id],
                label: arr[i][label],
                iconType: arr[i].iconType,
                children: getChildren(arr[i].children)
            }
            if (item.children && !item.children.length) {
                delete item.children
            }
            newArr.push(item)
        }
    }
    return newArr
}
/**
 * 递归查找数目录里面id对应的数据
 * @param arr
 * @param key        查找的字段
 * @param id        查找的id
 * @returns {Object}
 */
export const recursion = (arr, key, id) => {
    let result = null
    if (!arr) {
        return
    }
    for (var i = 0; i < arr.length; i++) {
        let item = arr[i]
        if (item[key] === id) {
            result = item
            break
        } else if (item.children && item.children.length > 0) {
            result = recursion(item.children, key, id)
            if (result) {
                return result
            }
        }
    }
    return result
}
/**
 * 分割数组创建二维数组封装
 * @param data 数组
 * @param senArrLen 需要分割成子数组的长度
 */
export const splitArr = (data, senArrLen) => {
    // 处理成len个一组的数据
    let dataLen = data.length
    let arrOuterLen = dataLen % senArrLen === 0 ? dataLen / senArrLen : parseInt((dataLen / senArrLen) + '') + 1
    let arrSecLen = dataLen > senArrLen ? senArrLen : dataLen // 内层数组的长度
    let arrOuter = new Array(arrOuterLen) // 最外层数组
    let arrOuterIndex = 0 // 外层数组的子元素下标
    for (let i = 0; i < dataLen; i++) {
        if (i % senArrLen === 0) {
            arrOuterIndex++
            let len = arrSecLen * arrOuterIndex
            // 将内层数组的长度最小取决于数据长度对len取余，平时最内层由下面赋值决定
            arrOuter[arrOuterIndex - 1] = new Array(dataLen % senArrLen)
            if (arrOuterIndex === arrOuterLen) len = dataLen % senArrLen === 0 ? dataLen % senArrLen + senArrLen * arrOuterIndex : dataLen % senArrLen + senArrLen * (arrOuterIndex - 1)
            let arrSecIndex = 0 // 第二层数组的索引
            for (let k = i; k < len; k++) { // 第一层数组的开始取决于第二层数组长度*当前第一层的索引
                arrOuter[arrOuterIndex - 1][arrSecIndex] = data[k]
                arrSecIndex++
            }
        }
    }
    return arrOuter
}
// splitArr([1, 2, 3, 4, 5, 6, 7, 8], 3) // [Array(3), Array(3), Array(2)] 0:(3) [1, 2, 3] 1:(3) [4, 5, 6] 2:(2) [7, 8]
// splitArr([1, 2, 3, 4, 5, 6], 3) // [Array(3), Array(3)] 0:(3) [1, 2, 3] 1:(3) [4, 5, 6]
